
#include "../include/tsmp_api.h"
//#include "../../libsim/include/sim.h"

//#define WRITE_FILE_PATH "data110.txt"
//#define WRITE_FILE_STATE_PATH "data210.txt"
//#define READ_FILE_PATH "data010.txt"
//#define TIME_TXT  "time.txt"

#define CONFIG_FILE "lib.cfg"

//文本最多1024行
#define MAX_LINES 1024

//文本每行最大256字节
#define LINE_LEN  256

#define LINE(x)   file.lines[x].line
#define LINE_CNT  file.line_cnt

//读写文本相关结构体
struct one_line {
	char line[LINE_LEN];
	int char_cnt;
};

//读写文本相关结构体
struct file_content {
	struct one_line lines[MAX_LINES];
	int line_cnt;
};

static void riprt(char *str)
{
	int len, i;

	if (str == NULL)
		return;
	len = strlen(str);
	if (len == 0) 
		return;

	for (i = 0; i < len; i++)
	{
		if (str[i] == '\r' || str[i] == '\n')
			str[i] = '\0';
    }
}

int get_cfgx_file(const char *filpath, const char *nam, char *val)
{
	FILE *fd = NULL;
	struct file_content file;
    char *saveptr1;
    char *saveptr2;
    char *pstr = NULL;
    int i = 0;
    int ret = -1;

    if ( (NULL == filpath) || (NULL == nam) || (NULL == val))
		return ret;
        
    fd = fopen(filpath, "r");
    if (fd == NULL)
		return ret;
    memset(&file, 0, sizeof(file));
        
        //read file stream to memory
    while (fgets(LINE(LINE_CNT), LINE_LEN, fd) != NULL)
		LINE_CNT++;

        //compare and put out the value
    for (i = 0; i < LINE_CNT; i++)
	{
        if (LINE(i)[0] == '#')
            continue;
                
        if ((pstr = strtok_r(LINE(i), "=", &saveptr1)) != NULL)
		{
			if (!strcmp(pstr, nam))
			{
				pstr = strtok_r(NULL, "=", &saveptr1);
                strcpy(val, pstr);
				riprt(val);
                ret = 0;
                break;
            }
        }
                
		if ((pstr = strtok_r(LINE(i), " ", &saveptr2)) != NULL)
		{
            if (!strcmp(pstr, nam))
			{
				pstr = strtok_r(NULL, " ", &saveptr2);
                strcpy(val, pstr);
                riprt(val);
                ret = 0;
                break;
            }
        }
    }
    
	fclose(fd);
    return ret;
}



int tsmp_pkt_send_recv_init(u8* net_interface,struct sock_fprog filter)
{
	int ret = 0;
	int type = 0;

	unsigned char wirte_file[128] = {0};
	unsigned char wirte_file_state[128] = {0};
	unsigned char read_file[128] = {0};

	char tmp_buf[32] = { 0 };
	memcpy(tmp_buf,net_interface,sizeof(tmp_buf));

	type = atoi(tmp_buf);

	get_cfgx_file(CONFIG_FILE, "WRITE_FILE_PATH", wirte_file);
	get_cfgx_file(CONFIG_FILE, "WRITE_FILE_STATE_PATH", wirte_file_state);
	get_cfgx_file(CONFIG_FILE, "READ_FILE_PATH", read_file);

	//printf("wirte_file=%s",wirte_file);
	//printf("wirte_file_state=%s",wirte_file_state);
	//printf("read_file=%s",read_file);
	
	ret = data_pkt_send_init(wirte_file,wirte_file_state);
	ret = data_pkt_receive_init(read_file,type);
	
	return ret;	
}


/*
	定义：int data_pkt_send_handle(u8* pkt,u16 len);
	功能：完成数据报文的发送处理
	输入参数：数据报文指针、数据报文长度
	返回结果：成功返回0，失败返回-1
*/

int tsmp_pkt_send_handle(u8* pkt,u16 len)
{
	data_pkt_send_handle(pkt,len);
	return 0;
}

u8 *tsmp_pkt_receive_dispatch_1(u16 *len)
{
	u8 *pkt = NULL;
	
	pkt = data_pkt_receive_dispatch_1(len);
	return pkt;
	
}


int tsmp_pkt_send_recv_destroy()
{
	data_pkt_send_destroy();
	data_pkt_receive_destroy();
	
	return 0;
}



